package cn.zyq.stater.config3.autoconfig;

import cn.zyq.stater.bean.User1;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration//本类作为配置类，并且在spring.factories中进行配置了，如果本项目作为jar引入到其他项目中了
//即使不启动本项目, 引入本jar的项目启动时会根据spring.factories加载本配置类.然后根据 @ConditionalOnProperty判断是否会执行createUser1方法
@PropertySource(value = "jdbc.properties", ignoreResourceNotFound=false)
@Slf4j
public class ZyqAutoConfigure {

    //本类ZyqAutoConfigure作为配置类是肯定会被加载到内存的，但本方法createUser1()在加载时是否会执行要看@ConditionalOnProperty条件是否成立
    @Bean
    //首先要加载了本配置类,  然后还要条件满足,  就会创建Bean对象(User1类型),(条件不满足,user对象不创建<需要用到jdbc.properties中的配置信息>)
    @ConditionalOnProperty(prefix = "zyq", value = "enable", havingValue = "true" , matchIfMissing = false)
    public User1 createUser1(){
        log.info("条件满足1，createUser1()创建User1对象");
        System.err.println("条件满足2，createUser1()创建User1对象");
        return  new User1();
    }

}